Imports System.Windows.Forms

Imports System.IO

Imports System.Collections.ObjectModel
Public Class DlgPreferences

    Private LogFile As New Logging.FileLogTraceListener
    Private strSelectedPort As String
    ' Private strSerialCommunication As Boolean = False
    Private strSelectedDrive As String
    Private strSelectedDirectory As String


    Public Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        LogFile.BaseFileName = "C:\windows\temp\Preferences.txt"
    End Sub

    Protected Property SelectedPort() As String
        Get
            SelectedPort = strSelectedPort
        End Get
        Set(ByVal value As String)
            strSelectedPort = value
        End Set
    End Property

    Private Sub DisplayDrives()

        For Each Drive As DriveInfo In My.Computer.FileSystem.Drives
            Try
                Drive.RootDirectory.GetDirectories()
                Me.cboDrives.Items.Add(Drive.Name & " " & Drive.VolumeLabel)
            Catch ex As Exception
                LogFile.WriteLine("ERROR reading drives: " & ex.Message)
            End Try

        Next
        Me.cboDrives.SelectedIndex = 0
    End Sub

    Private Sub DlgPreferences_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        DisplayDrives()
        DisplayPorts()
    End Sub

    Private Sub cboDrives_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboDrives.SelectedIndexChanged
        Me.strSelectedDrive = Me.cboDrives.Text
        Me.strSelectedDirectory = ""
        DisplayDirectoriesAndFiles(Me.strSelectedDrive)
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim currentRow As String()
        Dim NewMessage As Boolean

        Using MyReader As New Microsoft.VisualBasic.FileIO.TextFieldParser("C:\Documents and Settings\pklein\Persoonlijk\Warns\GPSInput.txt")
            MyReader.TextFieldType = FileIO.FieldType.Delimited
            MyReader.SetDelimiters(",")

            While Not MyReader.EndOfData
                Try
                    NewMessage = True
                    currentRow = MyReader.ReadFields()
                Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
                    LogFile.WriteLine("Line " & ex.Message & "is invalid and will be skipped.")
                End Try
            End While
        End Using

    End Sub

    Private Sub DisplayPorts()
        Me.lstComPorts.Items.Clear()
        Try
            For Each SerialPortName As String In My.Computer.Ports.SerialPortNames
                lstComPorts.Items.Add(SerialPortName)  ' Show all available COM ports.
            Next
        Catch ex As Exception
            LogFile.WriteLine("ERROR displaying serial ports: " & ex.Message)
        End Try
    End Sub

    Private Sub lstComPorts_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstComPorts.SelectedIndexChanged
        Try
            MyPort = SerialPort1
            With MyPort

                If Me.lstComPorts.SelectedItem <> Me.SelectedPort Then
                    If Me.SelectedPort <> "" Then
                        .close()
                        Me.DisplayComportStatus(.isopen, .portname)
                    End If
                    Me.SelectedPort = Me.lstComPorts.SelectedItem
                    .portname = Me.SelectedPort
                    .BaudRate = 4800
                    .DataBits = 8
                    .Parity = IO.Ports.Parity.None
                    .StopBits = IO.Ports.StopBits.One
                    .Open()
                End If
                Me.DisplayComportStatus(.isopen, .portname)
            End With
        Catch ex As Exception
            LogFile.WriteLine("ERROR opening serial port: " & ex.Message)
        End Try
    End Sub

    Private Sub DisplayComportStatus(ByVal PortIsOpen As Boolean, ByVal Portname As String)
        Me.lblPortName.Text = Portname
        If PortIsOpen Then
            Me.PictureBox1.Image = My.Resources.bullet_ball_glass_green
        Else
            Me.PictureBox1.Image = My.Resources.bullet_ball_glass_red
        End If
    End Sub

    Private Sub btnClosePort_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClosePort.Click
        Try
            MyPort = SerialPort1
            With MyPort
                .close()
                Me.DisplayComportStatus(.isopen, .portname)
            End With
        Catch ex As Exception
            LogFile.WriteLine("ERROR closing the serial port: " & ex.Message)
        End Try

    End Sub

    Private Sub btnChoosePort_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnChoosePort.Click
        Try
            MyPort = SerialPort1
            With MyPort
                .close()
                GPSService.ManagementConsole.ActiveSerialPort = .portname
            End With
        Catch ex As Exception
            LogFile.WriteLine("ERROR while passing through the selected serial port: " & ex.Message)
        End Try

    End Sub

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        Me.Close()
    End Sub

    Private Sub DisplayDirectoriesAndFiles(ByVal strStartLocation As String)
        Dim NewTreeNode As New TreeNode
        Try
            Me.tvwDirectoryStructure.Nodes.Clear()

            NewTreeNode.Text = strStartLocation
            NewTreeNode.SelectedImageIndex = 0
            NewTreeNode.ImageIndex = 0
            Me.tvwDirectoryStructure.Nodes.Add(NewTreeNode)

            For Each MyDirectory As String In My.Computer.FileSystem.GetDirectories(strStartLocation, FileIO.SearchOption.SearchTopLevelOnly, "*.*")
                NewTreeNode.Nodes.Add(MyDirectory, Microsoft.VisualBasic.Right(MyDirectory, Len(MyDirectory) - 1 - MyDirectory.LastIndexOfAny("\")), 1, 2)
            Next

            For Each MyFile As String In My.Computer.FileSystem.GetFiles(strStartLocation, FileIO.SearchOption.SearchTopLevelOnly, "*.*")
                NewTreeNode.Nodes.Add(MyFile, Microsoft.VisualBasic.Right(MyFile, Len(MyFile) - 1 - MyFile.LastIndexOfAny("\")), 3, 3)
            Next

            Me.strSelectedDirectory = Microsoft.VisualBasic.Left(strStartLocation, Len(strStartLocation) - Len(Microsoft.VisualBasic.Right(strStartLocation, Len(strStartLocation) - 1 - strStartLocation.LastIndexOfAny("\"))))
            Me.tvwDirectoryStructure.ExpandAll()

        Catch ex As Exception
            LogFile.WriteLine("ERROR displaying directories and files: " & ex.Message)
        End Try
    End Sub

    Private Sub tvwDirectoryStructure_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tvwDirectoryStructure.DoubleClick
        Dim ActiveNode As TreeView
        Try
            ActiveNode = sender
            If Not ActiveNode.SelectedNode.Text = "" Then
                DisplayDirectoriesAndFiles(ActiveNode.SelectedNode.Name)
            End If
        Catch ex As Exception
            LogFile.WriteLine("ERROR processing selected directory of file: " & ex.Message)
        End Try

    End Sub

    Private Sub btnSelectFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSelectFile.Click
        GPSService.ManagementConsole.SourceFile = Me.tvwDirectoryStructure.SelectedNode.Name
    End Sub

    Private Sub btnPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrevious.Click
        If Not strSelectedDirectory = "" Then
            DisplayDirectoriesAndFiles(strSelectedDirectory)
        End If
    End Sub

End Class
